from django_filters import rest_framework as filters
from .models import Category, Tag, Link, UserVisit, UserPreference

class CategoryFilter(filters.FilterSet):
    name = filters.CharFilter(lookup_expr='icontains')
    parent = filters.NumberFilter(field_name='parent', lookup_expr='exact')
    
    class Meta:
        model = Category
        fields = ['name', 'parent']

class TagFilter(filters.FilterSet):
    name = filters.CharFilter(lookup_expr='icontains')
    
    class Meta:
        model = Tag
        fields = ['name']

class LinkFilter(filters.FilterSet):
    title = filters.CharFilter(lookup_expr='icontains')
    url = filters.CharFilter(lookup_expr='icontains')
    category = filters.NumberFilter(field_name='category', lookup_expr='exact')
    tags = filters.NumberFilter(field_name='tags', lookup_expr='exact')
    min_visit_count = filters.NumberFilter(field_name='visit_count', lookup_expr='gte')
    max_visit_count = filters.NumberFilter(field_name='visit_count', lookup_expr='lte')
    is_active = filters.BooleanFilter()
    
    class Meta:
        model = Link
        fields = ['title', 'url', 'category', 'tags', 'is_active']

class UserVisitFilter(filters.FilterSet):
    user = filters.NumberFilter(field_name='user', lookup_expr='exact')
    link = filters.NumberFilter(field_name='link', lookup_expr='exact')
    date_from = filters.DateFilter(field_name='created_at', lookup_expr='gte')
    date_to = filters.DateFilter(field_name='created_at', lookup_expr='lte')
    
    class Meta:
        model = UserVisit
        fields = ['user', 'link']

class UserPreferenceFilter(filters.FilterSet):
    user = filters.NumberFilter(field_name='user', lookup_expr='exact')
    theme = filters.CharFilter(lookup_expr='exact')
    layout = filters.CharFilter(lookup_expr='exact')
    
    class Meta:
        model = UserPreference
        fields = ['user', 'theme', 'layout']
